<template>
  <van-dialog
    v-model="visible"
    show-cancel-button
    :beforeClose="handleSubmit"
    confirm-button-color="rgba(25, 137, 250, 1)"
  >
    <div class="ip-container">
      <ag-grid-vue
        style="width: 100%; height: 68vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        :singleClickEdit="true"
        rowSelection="multiple"
        :rowData="ShowDataNum"
      >
      </ag-grid-vue>
    </div>
  </van-dialog>
</template>

<script>
import { Dialog } from "vant";
import { AgGrid } from "../mixins/AgGrid";
import { AgGridVue } from "ag-grid-vue";
import { toast } from "../utils/common";

export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
    AgGridVue,
  },
  mixins: [AgGrid],
  watch: {
    value(val) {
      this.visible = val;
    },
    visible(val) {
      this.$emit("input", val);
    },
    ShowDataNum(val) {
      this.ShowDataNum = val;
      console.log(this.ShowDataNum);
    },
  },
  props: {
    //父给子传递
    value: {
      type: Boolean,
      required: true,
    },
    ShowDataNum: {
      type: Array,
    },
  },
  data() {
    return {
      savevalue: "",
      isno: false,
      visible: this.value,
      columnDefs: [
        { headerName: "物料号", field: "productid", width: 85 },
        { headerName: "配比", field: "bl", width: 70 },
        { headerName: "投产数", field: "num", width: 75 },
        { headerName: "块数", field: "blocknum", width: 80 },
      ],
    };
  },
  methods: {
    handleSubmit(action, done) {
      if (action === "confirm") {
        this.checkFormula();
        console.log(this.isno);
        if (this.isno != false) {
          this.isno = true;
          this.$emit("PFBNum");
          done();
        } else {
          toast("不成比例，无法结束当前批次");
          this.isno = true;
          done(false);
        }
      } else {
        this.$emit("PFBcancel");
        done();
      }
    },
    checkFormula() {
      console.log("1");
      console.log(this.ShowDataNum);
      let NUmNew = this.ShowDataNum[0].numnew;
      for (var i = 0; i <= this.ShowDataNum.length - 1; i++) {
        if (NUmNew !== this.ShowDataNum[i].numnew) {
          this.isno = false;
        }
        if (this.ShowDataNum[i].numnew == 0) {
          this.isno = false;
        }
      }
    },
  },
  mounted() {
    //组件进来将父组件给的value值给visible
    this.visible = this.value;
    console.log(this.visible);
  },
};
</script>

<style scoped lang="less">
.ip-container {
  padding: 1rem;
}
</style>